/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       garrison_button.cpp

	$Header: /game/garrison_button.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "garrison_button.h"

#include "bitmap_layer_window.h"
#include "creature_array_window.h"
#include "drag_garrison.h"
#include "interface_help.h"

// --------------------------------------------------------------------
// button-like window which allows player to drag entire army from one
// creature array window to another
// --------------------------------------------------------------------
t_garrison_button::t_garrison_button( t_bitmap_layer const* bitmap, t_screen_point point, 
		                              t_creature_array_window* army_window, 
									  t_select_handler select_handler, t_window* parent )
				 : t_window( k_some_transparency, parent )
{
	m_army_window = army_window;
	m_select_handler = select_handler;
	m_icon = new t_bitmap_layer_window( bitmap, t_screen_point(0,0), this, false );
	m_icon->set_visible( false );

	t_screen_rect rect = m_icon->get_client_rect() + point;

	init( rect );

	t_help_block const& help = get_help_block( "garrison_button" );

	set_help( this, help, "button" );
	set_help( m_icon, help, "button" );
}

// ---------------------------------------------------------------------------
// Make sure any drag we started ends before we go away
// ---------------------------------------------------------------------------
void t_garrison_button::on_close()
{
	// Note this must be done no later than this, but this may already
	// be too late because of dependencies on other peer windows or
	// resources managed by the parent.  In that case, call cancel_drag 
	// somewhere earlier in the parent's shutdown chain
	cancel_drag();
}

// --------------------------------------------------------------------
// button-like window which allows player to drag entire army from one
// creature array window to another
// --------------------------------------------------------------------
bool t_garrison_button::accept_drag( t_drag_object* drag, t_mouse_event const& event)
{
	t_counted_ptr<t_drag_garrison> drag_garrison = dynamic_cast<t_drag_garrison*>(drag);

	// check if this is the proper kind of drag.
	if (drag_garrison != 0)
	{
		t_garrison_button* source = drag_garrison->get_source_window();

		if (source == this)
			return true;

		// get armies
		t_creature_array_window* source_creature_window = source->m_army_window;
		t_creature_array* source_army = source_creature_window->get_army();
		t_creature_array* dest_army = m_army_window->get_army();

		// cancel if both are the same.
		if (source_army == dest_army)
			return true;

		// try and add source to dest, preferably in the same slot.
		m_army_window->merge( source_creature_window );
		// inform drag it's been accepted.
		drag_garrison->accepted( this );
		// select this item.
		m_icon->set_visible( true );
		m_select_handler( this );
		return true;
	}
	return t_window::accept_drag( drag, event );
}

// --------------------------------------------------------------------
// button-like window which allows player to drag entire army from one
// creature array window to another
// --------------------------------------------------------------------
void t_garrison_button::drag_event( t_mouse_event const& event )
{
	t_drag_garrison* drag = new t_drag_garrison( m_icon->get_bitmap(), this );

	m_icon->set_visible( false );
	begin_drag( drag );
}

// --------------------------------------------------------------------
// button-like window which allows player to drag entire army from one
// creature array window to another
// --------------------------------------------------------------------
void t_garrison_button::left_button_down( t_mouse_event const& event )
{
	highlight( true );
	m_icon->set_visible( true );
	m_select_handler( this );
}

// --------------------------------------------------------------------
// button-like window which allows player to drag entire army from one
// creature array window to another
// --------------------------------------------------------------------
void t_garrison_button::left_button_up( t_mouse_event const& event )
{
	highlight( false );
}

void t_garrison_button::highlight( bool arg )
{
	t_creature_array const* army = m_army_window->get_army();
	int i;

	for (i = 0; i < t_creature_array::k_size; i++)
		m_army_window->set_highlight( i, arg && (*army)[i].get_number() > 0);
}

// --------------------------------------------------------------------
// button-like window which allows player to drag entire army from one
// creature array window to another
// --------------------------------------------------------------------
void t_garrison_button::select( bool arg )
{
	m_icon->set_visible( arg );

	if (!arg)
		highlight( false );
}
